const { ipcRenderer } = require('electron');
const log = require('electron-log');

/**
 * FTP客户端工具类
 */
class FTPClient {
  constructor() {
    this.host = process.env.FTP_HOST || 'drive.kbin.cc';
    this.port = parseInt(process.env.FTP_PORT) || 21;
    this.username = null;
    this.password = null;
    this.isConnected = false;
  }

  /**
   * 设置FTP连接信息
   */
  setCredentials(username, password, host = process.env.FTP_HOST || 'drive.kbin.cc', port = parseInt(process.env.FTP_PORT) || 21) {
    this.username = username;
    this.password = password;
    this.host = host;
    this.port = port;
  }

  /**
   * 连接到FTP服务器
   */
  async connect() {
    try {
      const result = await ipcRenderer.invoke('ftp-connect', {
        host: this.host,
        port: this.port,
        user: this.username,
        password: this.password
      });

      if (result.success) {
        this.isConnected = true;
        log.info('FTP连接成功');
        return true;
      } else {
        log.error('FTP连接失败:', result.error);
        return false;
      }
    } catch (error) {
      log.error('FTP连接错误:', error);
      return false;
    }
  }

  /**
   * 断开FTP连接
   */
  async disconnect() {
    try {
      await ipcRenderer.invoke('ftp-disconnect');
      this.isConnected = false;
      log.info('FTP已断开连接');
    } catch (error) {
      log.error('FTP断开连接错误:', error);
    }
  }

  /**
   * 上传文件
   */
  async uploadFile(localPath, remotePath, onProgress) {
    try {
      if (!this.isConnected) {
        await this.connect();
      }

      const result = await ipcRenderer.invoke('ftp-upload', {
        localPath,
        remotePath
      });

      if (result.success) {
        log.info(`FTP上传成功: ${localPath} -> ${remotePath}`);
        return { success: true };
      } else {
        log.error('FTP上传失败:', result.error);
        return { success: false, error: result.error };
      }
    } catch (error) {
      log.error('FTP上传错误:', error);
      return { success: false, error: error.message };
    }
  }

  /**
   * 下载文件
   */
  async downloadFile(remotePath, localPath, onProgress) {
    try {
      if (!this.isConnected) {
        await this.connect();
      }

      const result = await ipcRenderer.invoke('ftp-download', {
        remotePath,
        localPath
      });

      if (result.success) {
        log.info(`FTP下载成功: ${remotePath} -> ${localPath}`);
        return { success: true };
      } else {
        log.error('FTP下载失败:', result.error);
        return { success: false, error: result.error };
      }
    } catch (error) {
      log.error('FTP下载错误:', error);
      return { success: false, error: error.message };
    }
  }

  /**
   * 创建远程目录
   */
  async createDirectory(remotePath) {
    try {
      if (!this.isConnected) {
        await this.connect();
      }

      const result = await ipcRenderer.invoke('ftp-mkdir', { remotePath });
      return result;
    } catch (error) {
      log.error('FTP创建目录错误:', error);
      return { success: false, error: error.message };
    }
  }

  /**
   * 检查连接状态
   */
  isConnectedToFTP() {
    return this.isConnected;
  }
}

module.exports = new FTPClient();
